package com.monkey.banana.game.shudu;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.monkey.banana.R;

/**
 * Description: 九宫格View
 * Author: Archur
 * Date: 2017-09-15
 * Time: 17:07
 *
 * 功能介绍:
 * 1、
 */

public class ShuduView extends View {

    private static final String TAG = "ShuduView";

    // 单元格的宽高
    private float width;
    private float height;

    //画笔
    Paint backgroundPaint;
    Paint lightPaint;
    Paint darkPaint;
    Paint numberPaint;

    private Game game = new Game();
    private Context context;

    public ShuduView(Context context) {
        this(context, null);
    }

    public ShuduView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShuduView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        backgroundPaint = new Paint();
        backgroundPaint.setColor(this.getResources().getColor(R.color.shudu_bg));

        lightPaint = new Paint();
        lightPaint.setStrokeWidth(3);
        lightPaint.setColor(this.getResources().getColor(R.color.light_line));

        darkPaint = new Paint();
        darkPaint.setStrokeWidth(5);
        darkPaint.setColor(this.getResources().getColor(R.color.dark_line));

        numberPaint = new Paint();
        numberPaint.setColor(Color.BLACK);
//        numberPaint.setStyle(Paint.Style.STROKE); //让其画出来的东西是 空的
        numberPaint.setTextSize(60); //设置字体大小
//        numberPaint.setTextAlign(Paint.Align.CENTER); //让字体居中
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        //取宽高的较小值设为9宫格的边长
        if (w < h) {
            this.width = w / 9f;
            this.height = w / 9f;
        } else {
            this.width = h / 9f;
            this.height = h / 9f;
        }
        super.onSizeChanged(w, h, oldw, oldh);
    }

    /**
     * 设置为一个正方形
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int min = Math.min(widthSize, heightSize);

        setMeasuredDimension(min, min);
    }

    @Override
    public void onDraw(Canvas canvas) {

        // 1 画View的背景
        canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backgroundPaint);

        // 2 画九宫格的纵横细线
        for (int i = 0; i < 9; i++) {
            canvas.drawLine(0, i * height, this.getWidth(), i * height, lightPaint);

            canvas.drawLine(i * width, 0, i * width, this.getHeight(), lightPaint);
        }
        // 3 画九宫格的纵横粗线
        for (int i = 0; i < 4; i++) {
            canvas.drawLine(0, i * height * 3, this.getWidth(), i * height * 3, darkPaint);

            canvas.drawLine(i * width * 3, 0, i * width * 3, this.getHeight(), darkPaint);
        }

        // 4 根据数据源往九宫格中绘制数字
        float x = width / 2f;// 初始x方向偏移量(还没有减去数字文本的长度的一半)
        Paint.FontMetrics fm = numberPaint.getFontMetrics();
        float y = height / 2f - (fm.ascent + fm.descent) / 2; // 初始y方向偏移量
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                String num = game.getTileString(i, j);
                float v = numberPaint.measureText(num);
                canvas.drawText(num, i * width + x - v / 2f, j * height + y, numberPaint);
            }
        }

        super.onDraw(canvas);
    }

    int selectedX;
    int selectedY;

    /**
     * 点击某一个单元格，弹窗选择数字
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return super.onTouchEvent(event);
        }

        // 将具体坐标转换成(2,7)这类坐标
        selectedX = (int) (event.getX() / width);
        selectedY = (int) (event.getY() / width);

        //把获取某单元格 不可用的数据
        int[] used = game.getUsedTilesByCoor(selectedX, selectedY);

        for (int i = 0; i < used.length; i++) {
            Log.e(TAG, "onTouchEvent:" + used[i]);
        }

        KeyDialog keyDialog = new KeyDialog(this.getContext(), used, this);

        keyDialog.show();

        return true;
    }

    //刷新整个九宫格里的数据
    public void setSelectedTile(int tile) {
        if (game.setTileIfValid(selectedX, selectedY, tile)) {
            invalidate();//重新绘制整个视图，也就相当于 重新生成一次该对象
        }

        //游戏胜利条件判断
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                // 这里的 数组 赋值，只要 它们 加起来 是 三维 即可，假如是 四维 也一样
                String tileString = game.getTileString(i, j);
                int[] cannotUse = game.getUsedTilesByCoor(i, j);
                if (cannotUse.length != 8 || TextUtils.isEmpty(tileString)){
                    return;
                }
            }
        }
        Toast.makeText(context,"游戏胜利",Toast.LENGTH_SHORT).show();
    }
}
